﻿using System.Data;
using System.Web.UI.WebControls;
using H2.NET.Common;
using H2.NET.Web.Management.Config.Service;
using H2.NET.Web.Pages.SMD.Helper;
using H2.NET.Web.Workflow.DwzCode;
using System;
using H2.NET.Web.Workflow.Helper;

namespace H2.NET.Web.Pages.SMD
{
    public partial class SelectDeposit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            switch (AksRequest.GetString("action"))
            {
                case "create":
                {

                    var region = AksRequest.GetFormString("ddl_region");
                    var province = AksRequest.GetFormString("ddl_province");
                    var channel = AksRequest.GetFormString("channel");

                    var applicationNo = GenerateApplicationNo();
                    var autoBusinessKey = AksRequest.GetString("AutoBusinessKey");
                    string summary = "上传汇款底单/保证金批量返还";

                    if (!string.IsNullOrEmpty(autoBusinessKey))
                    {
                        if(autoBusinessKey.Equals(Constant.PROCESS_DEVICEISSUED))
                            summary = "上传汇款底单/设备发放";
                        TaskAPI["AutoBusinessKey"] = autoBusinessKey;
                    }
                    TaskAPI["IsDevice"] = "1";
                    TaskAPI["IsRenovation"] = "0";
                    TaskAPI["BlankValue"] = "0";
                    TaskAPI["Channel"] = channel;
                    TaskAPI.InitTask(UserAPI, applicationNo);
                    TaskAPI.Summary = summary;

                    TaskAPI.SetDepositChangeRecipient(region, province);
                    TaskAPI["Region"] = region;
                    TaskAPI["Province"] = province;
                    TaskAPI["R_Submit_Document"] = new object[1] {UserAPI.UserName};

                    if (TaskAPI.Send())
                    {
                        var url = string.Format("pages/smd/SelectDeposit.aspx?action=redirect&incident={0}",
                            TaskAPI.Incident);
                        SuccessMsg(url);
                    }
                    else
                    {
                        ErrorMsg("出现异常");
                    }
                }
                    break;
                case "redirect":
                {
                    var incidentNo = AksRequest.GetInt("incident", 0);
                    Logging.GetLogger().Info("保证金变更");
                    var task = TaskHelper.GetTask("Store Deposit Change", incidentNo, "Submit Document");

                    if (task != null)
                    {
                        var url = string.Format("BatchDepositChange.aspx?taskid={0}", task.strTaskId);
                        Response.Redirect(url);
                    }
                    else
                    {
                        Response.Redirect("/SystemError.aspx");
                    }
                }
                    break;
                default:
                    InitSelect();


                    var key = AksRequest.GetQueryString("AutoBusinessKey");
                    if (!string.IsNullOrEmpty(key))
                    {
                        AutoBusinessKey.Value = key;
                    }

                    break;
            }
        }

        protected void InitSelect()
        {
            var configService = new ConfigService();
            #region 门店区域
            var dtRegion = configService.GetList("ParentId = (select configid from t_smd_Config where code='M_MDQY') order by seq").Tables[0];
            ddl_region.Items.Add(new ListItem("==请选择==", ""));
            foreach (DataRow row in dtRegion.Rows)
            {
                ddl_region.Items.Add(new ListItem(row["Name"].ToString(), row["Name"].ToString()));
            }
            #endregion


            #region 门店省份
            ddl_province.Items.Add(new ListItem("==请选择==", ""));
            #endregion
        }
    }
}